/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-09-05
 * Time: 9:19
 */
class Solution2 {
    public boolean backspaceCompare(String s, String t) {
        return change(s).equals(change(t));
    }

    public String change(String s) {
        int n = s.length();
        StringBuilder ret = new StringBuilder();

        for(int i = 0; i < n; i++) {
            // 进栈
            if(s.charAt(i) != '#') {
                ret.append(s.charAt(i));
            } else {
                // 出栈
                if(ret.length() > 0) {
                    ret.deleteCharAt(ret.length()-1);
                }
            }
        }
        return ret.toString();
    }
}

